//
//  ForgetPasswordViewController.m
//  XRSquare
//
//  Created by 穆相臣 on 2018/8/29.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "TokenViewController.h"

@interface ForgetPasswordViewController ()
{
    UITextField * phone;
    UITextField * code;
    UIImageView * codei;
}
@end

@implementation ForgetPasswordViewController

-(void)tapCancel
{
    [phone resignFirstResponder];
    [code resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backImage.image = [UIImage imageNamed:@"lb"];
    backImage.userInteractionEnabled = YES;
    [self.view addSubview:backImage];
    
    UITapGestureRecognizer * tapCancel = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCancel)];
    [backImage addGestureRecognizer:tapCancel];
    
    UIButton * backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 40, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    UILabel * createAccount = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, ScreenWidth - 40, 30)];
    createAccount.font = [UIFont boldSystemFontOfSize:15];
    createAccount.text = @"忘记密码";
    [self.view addSubview:createAccount];
    
    phone = [[UITextField alloc]initWithFrame:CGRectMake(20, 170, ScreenWidth - 40, 30)];
    phone.placeholder = @"手机号";
    [self.view addSubview:phone];
    
    UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(20, 200, ScreenWidth - 40, 1)];
    line2.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:line2];
    
    code = [[UITextField alloc]initWithFrame:CGRectMake(20, 250, ScreenWidth - 40 - 60, 30)];
    code.placeholder = @"验证码";
    [self.view addSubview:code];
    
    UIView * line3 = [[UIView alloc]initWithFrame:CGRectMake(20, 280, ScreenWidth - 40, 1)];
    line3.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [self.view addSubview:line3];
    
    codei = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 80 - 40, 240, 100, 40)];
    codei.userInteractionEnabled = YES;
    [self.view addSubview:codei];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(codeGenerate)];
    [codei addGestureRecognizer:tap];
    
    UIButton * next = [[UIButton alloc]initWithFrame:CGRectMake(20, 320, ScreenWidth - 40, 40)];
    next.layer.borderColor = [UIColor orangeColor].CGColor;
    next.layer.borderWidth = 1;
    next.layer.cornerRadius = 20;
    [next setTitleColor:themeColor forState:UIControlStateNormal];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    [next addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:next];
    
    [self codeGenerate];
}

-(void)codeGenerate
{
    [NetRequestCenter makecaptchaResult:^(BOOL success, NSDictionary *resultDic) {
        NSLog(@"%@",resultDic);
        NSString * content = [resultDic objectForKey:@"content"];
        NSString * base64String = [[[resultDic objectForKey:@"img"]componentsSeparatedByString:@","]lastObject];
        NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:base64String options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
        UIImage *decodedImage = [UIImage imageWithData: decodeData];
        codei.image = decodedImage;
    }];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)nextAction
{
    [NetRequestCenter changePasswordWithS:@"1" mobile:phone.text password:@"" captcha:code.text checktype:@"0" result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            NSLog(@"%@",resultDic);
            TokenViewController * token = [[TokenViewController alloc]init];
            token.phone = phone.text;
            token.fp = YES;
            [self.navigationController pushViewController:token animated:YES];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

